package com.cjm.pattern.p1;

import com.cjm.pattern.p1.origin.BigPenguin;
import com.cjm.pattern.p1.origin.LittlePenguin;
import com.cjm.pattern.p1.origin.MiddlePenguin;
import com.cjm.pattern.p1.pattern.BigPenguinOne;
import com.cjm.pattern.p1.pattern.LittlePenguinOne;
import com.cjm.pattern.p1.pattern.MiddlePenguinOne;
import com.cjm.pattern.p1.strategy.BehaviorContext;
import com.cjm.pattern.p1.strategy.Big;
import com.cjm.pattern.p1.strategy.Little;
import com.cjm.pattern.p1.strategy.Middle;
import com.cjm.ry.jvm.bean.Person;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 定义：一个抽象类公开定义了执行了执行它的方法的方式、模板， 它的子类可以按需要重写方法实现，
 * 但调用将以抽象类中定义的方式进行， 属于行为型模式。
 */
public class Test {
    public static void main(String[] args) {
        zero();
        System.err.println("====================");
        one();
        System.err.println("====================");
        two();
    }

    /**
     * 策略
     */
    private static void two() {
        BehaviorContext behaviorContext = new BehaviorContext(new Little());
        behaviorContext.everyDay();

        behaviorContext.setPenguin(new Middle());
        behaviorContext.everyDay();

        behaviorContext.setPenguin(new Big());
        behaviorContext.everyDay();


        List<Person> persons = Collections.synchronizedList(new ArrayList<>(1000000));
        AtomicBoolean run = new AtomicBoolean(true);
        new Thread(() -> {
            while (run.get()) {
                try {
                    persons.add(new Person());
                    if (persons.size() == 1000000) {
                        persons.clear();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            String line = scanner.next();
            if ("stop".equals(line)) {
                run.set(false);
                break;
            }
        }
        System.err.println("over");
    }

    /**
     * 模板方法
     */
    private static void one() {
        LittlePenguinOne littlePenguinOne = new LittlePenguinOne();
        MiddlePenguinOne middlePenguinOne = new MiddlePenguinOne();
        BigPenguinOne bigPenguinOne = new BigPenguinOne();

        littlePenguinOne.everyDay();
        middlePenguinOne.everyDay();
        bigPenguinOne.everyDay();
    }

    /**
     * 原始方式
     */
    private static void zero() {
        System.err.println("little");
        LittlePenguin littlePenguin = new LittlePenguin();
        littlePenguin.everyDay();


        System.err.println("middle");
        MiddlePenguin middlePenguin = new MiddlePenguin();
        middlePenguin.everyDay();


        System.err.println("big");
        BigPenguin bigPenguin = new BigPenguin();
        bigPenguin.everyDay();
    }
}
